﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Users;
using BLL.Users.Impl;
using Model;
using BLL.Info;
using BLL.Info.Impl;
using BLL.ApplicationConfiguration;
using Utils;
using BLL.Comment;
using BLL.Comment.Impl;

public partial class Page_MasterPage : System.Web.UI.MasterPage
{
    //用户业务操作接口
    private UserBO userBO = new UserBOImpl();
    //获取当前路径
    string path = "";

    //加载初始化
    protected void Page_Load(object sender, EventArgs e)
    {
        //设置页面
        ImagePath.Value = WebUtil.GetImagesCurrentPath(Request.Url);

        //显示事件信息Top10列表
        ShowNewsTop10();
        //显示出版物信息Top10列表
        ShowPublicationsTop10();
        //显示专家信息Top10列表
        ShowExpertsTop10();
        //显示网站评论信息Top10列表
        ShowCommentsTop10();
       
        //
        path = WebUtil.GetCurrentPath(Request.Url);

        //判断用户是否登录
        string userId = WebUtil.GetIdentifier(Session["userId"]);
        if (!userId.Equals(""))
        {
            //隐藏登录DIV,显示菜单栏DIV
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", GenerateScript(true));
            lblMasterUserName.Text = userId;

            //初始化用户菜单栏
            InitializationUserMenu(userId.ToString());
         }
        else
        {
            //显示登录DIV,隐藏菜单栏DIV
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", GenerateScript(false));
        }
    }
 
    //用户登录
    protected void btnMasterLogin_Click(object sender, ImageClickEventArgs e)
    {
        //收集数据
        string userName = txtMasterLoginUserName.Text.Trim();
        string userPassword = txtMasterLoginPassword.Text.Trim();
        //输入校验
        //数据封装
        User user = new User()
        {
            UserId=userName,
            UserPassword=userPassword
        };
        //调用接口
        string result = userBO.Login(user);
        //处理返回结果结果
        if (result == "Login success")
        {
            //将用户编号存入Session
            Session["userId"] = userName;
            //跳转到Home.aspx
            Response.Redirect(path+"Home.aspx");
        }
        else
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('" + result + "')</script>"); 
            Response.Write("<script>alert('" + result + "');window.location.href='" + path + "Home.aspx" + "'</script>");
        }
    }

    //注册
    protected void btnMasterRegister_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path+"Register/RegisterType.aspx");
    }

    //密码找回
    protected void linkMasterFofgottenPassword_Click(object sender, EventArgs e)
    {
        Response.Redirect(path + "Security/GetPasswordBack.aspx");
    }

    //登录注销
    protected void linkMasterLogout_Click(object sender, EventArgs e)
    {
        Session.Remove("userId");
        //跳转到Home.aspx
        Response.Redirect(path + "Home.aspx");
    }

    //生成js脚本
    private string GenerateScript(bool isShowLoginMaster)
    {
        //存放script的容器
        StringBuilder script = new StringBuilder();
        script.Append("<script>");

        if (isShowLoginMaster)
        {
            script.Append("document.getElementById('LoginForm').style.display='none';");
            script.Append("document.getElementById('Menu').style.display='block';");
        }
        else 
        {
            script.Append("document.getElementById('LoginForm').style.display='block';");
            script.Append("document.getElementById('Menu').style.display='none';");
        }

        script.Append("</script>");

        return script.ToString();
    }

  
    //显示事件信息Top10列表
    private void ShowNewsTop10()
    {
        EventBO eventBO = new EventBOImpl();
        rpEventsTop10.DataSource = eventBO.ShowTopEvent(10);
        rpEventsTop10.DataBind();
    }
    //显示出版物信息Top10列表
    private void ShowPublicationsTop10()
    {
        PublicationBO publicationBO = new PublicationBOImpl();
        rpPublicationsTop10.DataSource = publicationBO.ShowTopPublication(10);
        rpPublicationsTop10.DataBind();
    }
    //显示专家信息Top10列表
    private void ShowExpertsTop10()
    {
        ExpertBO expertBO = new ExpertBOImpl();
        rpExpertsTop10.DataSource = expertBO.ShowTopExpert(10);
        rpExpertsTop10.DataBind();
    }
    //显示网站评论信息Top10列表
    private void ShowCommentsTop10()
    {
        SiteCommentBO commentBO = new SiteCommentBOImpl();
        rpCommentsTop10.DataSource = commentBO.ShowTopSiteComment(10);
        rpCommentsTop10.DataBind();
    }
   
    //初始化用户菜单栏
    private void InitializationUserMenu(string userId)
    {
        //清楚用户菜单
        tvMasterMap.Nodes.Clear();

        Dictionary<string, List<UserMenu>> userMenu = RoleMenuConfiguration.GetRoleMenuMap(userId, path);

        //绑定TreeView控件的数据源
        foreach(KeyValuePair<string, List<UserMenu>> pair in userMenu)
        {
            TreeNode parentNode = new TreeNode();
            parentNode.Value = pair.Key;
            //展开当前所有节点
            parentNode.ExpandAll();
            //禁用一级菜单
            parentNode.NavigateUrl =""; //path + "Home.aspx"

            if (pair.Value != null && pair.Value.Count() > 0)
            {

                foreach (UserMenu menu in pair.Value)
                {
                    TreeNode childNode = new TreeNode();
                    childNode.Value = menu.MenuTitle;
                    childNode.NavigateUrl = menu.NavigateURL;
                    parentNode.ChildNodes.Add(childNode);
                }
            }

            tvMasterMap.Nodes.Add(parentNode);
        }

    }
    //新闻信息列表
    protected void btnMasterNews_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path + "News/NewsList.aspx");
    }

    //事件信息列表
    protected void btnMasterEvents_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path + "Events/EventsList.aspx");
    }

    //出版物信息列表
    protected void btnMasterPublications_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path + "Publications/PublicationsList.aspx");
    }
    //专家信息列表
    protected void btnMasterExperts_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path + "Expert/ExpertsList.aspx");
    }
    //网站评论信息列表
    protected void btnMasterComment_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path + "Comment/CommentsList.aspx");
    }
    //more事件列表
    protected void linkMasterEvents_Click(object sender, EventArgs e)
    {
        Response.Redirect(path + "Events/EventsList.aspx");
    }
    //more出版物列表
    protected void linkMasterPublications_Click(object sender, EventArgs e)
    {
        Response.Redirect(path + "Publications/PublicationsList.aspx");
    }
    //more专家列表
    protected void linkMasterExperts_Click(object sender, EventArgs e)
    {
        Response.Redirect(path + "Expert/ExpertsList.aspx");
    }
    //more评论列表
    protected void linkComment_Click(object sender, EventArgs e)
    {
        Response.Redirect(path + "Comment/CommentsList.aspx");
    }
    //事件详细信息
    protected void linkbtEvent_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string eventId = bt.CommandArgument.ToString();
        Response.Redirect(path + "Events/EventInfo.aspx?eventID=" + eventId);
        EventBO eventBO = new EventBOImpl();
        //eventBO.Update();
    }
    //出版物详细信息
    protected void linkbtPublication_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string publicationId = bt.CommandArgument.ToString();
        Response.Redirect(path + "Publications/PublicationInfo.aspx?publicationID=" + publicationId);
    }
    //专家详细信息
    protected void linkbtExpert_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string expertId = bt.CommandArgument.ToString();
        Response.Redirect(path + "Expert/ExpertDetails.aspx?expertId=" + expertId);
    }
    //评论详细信息
    protected void linkbtComment_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string commentId = bt.CommandArgument.ToString();
        Response.Redirect(path + "Comment/CommentDetails.aspx?commentId=" + commentId);
    }
    //搜索专家
    protected void btnMasterSearch_Click(object sender, ImageClickEventArgs e)
    {
        
        string expertName = txtMasterSearch.Text.Trim();
        if (expertName == "")
            return;
        Response.Redirect(WebUtil.GetCurrentPath(Request.Url) + "Expert/ExpertsList.aspx?expertName=" + expertName);
        
    }
    //返回主页
    protected void btnMasterHome_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(path + "Home.aspx");
    }

    public string GetCurrentPath()
    {
        return WebUtil.GetCurrentPath(Request.Url);
    }

}
